﻿
namespace Service
{
    using Data.Repository;
    using System.Linq;
	using System.Collections.Generic;

    public class RelatedProductService : IRelatedProductService
    {
        private IRelatedProductRepository _relatedProductRepository;

        #region IRelatedProductService Members

        /// <summary>
        /// create new RelatedProduct
        /// </summary>
        /// <param name="theNewRelatedProduct">the new RelatedProduct instance</param>
        /// <returns>the new RelatedProduct's ID</returns>
        public int CreateNewRelatedProduct(Data.Entity.RelatedProduct theNewRelatedProduct)
        {
            if (theNewRelatedProduct == null)
            {
                return 0;
            }

            if (_relatedProductRepository == null)
            {
                _relatedProductRepository = new RelatedProductRepository();
            }

            return _relatedProductRepository.CreateNewRelatedProduct(theNewRelatedProduct);
        }

        public int DeleteRelatedProduct(int productID1, int productID2)
        {
            if (productID1 <= 0 || productID2 <= 0)
            {
                return 0;
            }

            if (_relatedProductRepository == null)
            {
                _relatedProductRepository = new RelatedProductRepository();
            }

            return _relatedProductRepository.DeleteRelatedProduct(productID1, productID2);
        }

        public IList<Data.Entity.RelatedProduct> GetRelatedProducts(int productID)
        {
            if (productID <= 0)
            { 
                return null; 
            }

            if (_relatedProductRepository == null)
            {
                _relatedProductRepository = new RelatedProductRepository();
            }

            return _relatedProductRepository.GetRelatedProducts(productID);
        }

        public int UpdateRelatedProduct(Data.Entity.RelatedProduct theRelatedProduct)
        {
            if (theRelatedProduct == null || theRelatedProduct.RelatedProductID <= 0)
            {
                return 0;
            }

            if (_relatedProductRepository == null)
            {
                _relatedProductRepository = new RelatedProductRepository();
            }

            return _relatedProductRepository.UpdateRelatedProduct(theRelatedProduct);
        }

		public IList<Data.Entity.Product> GetRelatedProducts(System.Collections.Generic.IEnumerable<int> prodIDs)
		{
			if(prodIDs == null || prodIDs.Count() <= 0)
			{
				return null;
			}

			if(_relatedProductRepository == null)
			{
				_relatedProductRepository = new RelatedProductRepository();
			}

			return _relatedProductRepository.GetRelatedProducts(prodIDs);
		}

		#endregion
	}
}
